/*
 * Main.java
 *
 * Created on 20 marzec 2007, 01:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package client;

import data.Employee;
import data.Team;
import data.VacQuota;
import data.Vacation;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.List;
import java.util.Set;
import server.VacationServer;
import server.Constants;
import util.DatePeriod;
import util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author xavex
 */
public class DummyClient {
    
    /** Creates a new instance of Main */
    public DummyClient() {
    }
    
    public static void main(String[]  args)
    {
        Date d1 = new Date( 2000 - 1970, 2, 10 ); 
        Date d15= new Date( 2000 - 1970, 5, 10 ); 
        Date d2 = new Date( 2001 - 1970, 2, 10 ); 
        
        DatePeriod dp1 = new DatePeriod( d1, d2 );
        
        DatePeriod dp2 = new DatePeriod( d1, d2 );
        
        if( dp1.contains( d15 ) )
            System.out.println( "OK" );
        else
            System.out.println( "dupa" );
        
        //rmiTest();
   }



    private static void rmiTest() {
        // set the security manager for the client
        System.setSecurityManager(new RMISecurityManager());
        //get the remote object from the registry
        
        System.out.println("dupa1");
        try
        {
            System.out.println("Security Manager loaded");
            String url = "rmi://" + Constants.SERVER_ADDRESS + "/" + Constants.SERVER;
            VacationServer vacationServer = (VacationServer)Naming.lookup(url);
            System.out.println("Got remote object");
            System.out.println("connected with vacation server: " + vacationServer.getHelloWorld() );

  
            Set<Employee> emps = vacationServer.getEmployees();            
            int i=0;
//            Team team = new Team();
//            team.setName( "specjalny team" );
//            Team newTeam = vacationServer.saveTeam( team );
//            System.out.println( newTeam.getId() );
            
//            getTeamTest(vacationServer);
//            addEmpTest(vacationServer);
//            editEmpTest(vacationServer);
//            deleteTest(vacationServer);
            
        }
        catch (RemoteException exc) {
            System.out.println("Error in lookup: " + exc.toString()); 
        }
        catch (java.net.MalformedURLException exc) {
            System.out.println("Malformed URL: " + exc.toString());   
        }
        catch (java.rmi.NotBoundException exc)  {
            System.out.println("NotBound: " + exc.toString());
        }
        catch( Exception exc ) {
            System.out.println( exc );
        }
       
    }

    /**
     * @param vacationServer
     * @throws RemoteException
     */
    private static void getTeamTest(final VacationServer vacationServer)
             throws RemoteException {

        //operowanie na teamach
        Set<Team> teams = vacationServer.getTeams();           
        
        System.out.println("Number of teams: " + teams.size() + "\r\n");
        
        for( Team t : teams ) {
            System.out.println( t.getName() );
            
            t = vacationServer.reloadTeam( t, true, false );
               
            for( Employee e : t.getMembers() ) {
                System.out.println("   " + e.getName() + "\r\n" );
            }                    
        }
    }

    private static void addEmpTest(VacationServer vacationServer) throws java.rmi.RemoteException {
        Set<Team> teams = vacationServer.getTeams();
        
        Team team = teams.iterator().next();
        
        //dodaj mu nowego pracownika
        Employee emp = createEmployee(team);
        
        team = vacationServer.reloadTeam( team, true, false );
        team.getMembers().add( emp );
        
        vacationServer.saveTeam( team );
    }

    /**
     * @param team
     * @return
     */
    private static Employee createEmployee(final Team team) {
        Employee emp = new Employee();
        
        emp.setName( "pracownik teamu " + team.getName() + " " + new Date().toString() );
        emp.setTeam( team );

        return emp;
    }

    private static void editEmpTest(VacationServer vacationServer) throws RemoteException {
        Set<Team> teams = vacationServer.getTeams();
        Team team = teams.iterator().next();
        
        team = vacationServer.reloadTeam( team, true, false );
        Employee emp = team.getMembers().iterator().next();
        
        emp.setName( new Date().toString() );
        
        vacationServer.saveEmployee( emp );
    }

    private static void deleteTest(VacationServer vacationServer) throws RemoteException {
        Set<Team> teams = vacationServer.getTeams();
        Team team = teams.iterator().next();
        
        vacationServer.delete( team );
    }
}
